
class ArenaMap:
    def __init__(self, size):
        self.walls = []
        self.spawns = []
        self.exit = Exit((200, 200))
        self.navGraph = NavGraph()
        background = pygame.Surface(size)
        self.background = background.convert()

    def LoadMap(self, map_data):
        self.navGraph.set_dimensions(map_data.width, map_data.height)
        self.navGraph.alter_accessibility(map_data.inaccessible_nodes)
        self.GenerateWalls()
        self.CreateRenderGroup()
        self.spawns = []
        self.exit = Exit(map_data.exit)
        for i in map_data.spawns:
            self.spawns.append(SpawnPoint(Vector(*i)))

    def GenerateWalls(self):
        self.walls = []
        for x in range(self.navGraph.width - 1):
            y_run = -1
            for y in range(self.navGraph.height - 1):
                node1 = self.navGraph.get_node(x, y)
                node2 = self.navGraph.get_node(x + 1, y)
                if (node1.accessible == True and node2.accessible == False) or \
                      (node1.accessible == False and node2.accessible == True):
                    if y_run == -1:
                        y_run = y
                elif y_run != -1:
                    wall = Wall(Vector(x+1, y_run), Vector(x +1, y))
                    self.walls.append(wall)
                    y_run = -1
            
        for y in range(self.navGraph.height - 1):
            x_run = -1
            for x in range(self.navGraph.width - 1):
                node1 = self.navGraph.get_node(x, y)
                node2 = self.navGraph.get_node(x, y + 1)
                if (node1.accessible == True and node2.accessible == False) or \
                      (node1.accessible == False and node2.accessible == True):
                    if x_run == -1:
                        x_run = x
                elif x_run != -1:
                    wall = Wall(Vector(x_run, y+1), Vector(x, y + 1))
                    self.walls.append(wall)
                    x_run = -1
                
                
    def CreateRenderGroup(self):
        """ Generates a render group for map elements"""
        for x in range(self.navGraph.width):
            for y in range(self.navGraph.height):
                if self.navGraph.get_node(x, y).accessible == True:
                    colour = (255, 255, 255)
                else:
                    colour = (125, 125, 125)
                self.background.fill(colour, (x * 16, y * 16, 16, 16))
        for wall in self.walls:
            pygame.draw.line(self.background, (0, 0, 0),
                    tuple([x * 16 for x in wall.start.tuple()]),
                    tuple([x * 16 for x in wall.end.tuple()]))

    def CalculateCost(self, idx_1, idx_2):
        # FIXME
        return 100
    def Render(self, surface):
        surface.blit(self.background, (0, 0))
        self.exit.Render(surface)
        

